package com.cskaoyan.servlet;

import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.service.UserService;
import com.cskaoyan.service.UserServiceImpl;
import com.cskaoyan.util.JacksonUtil;
import com.cskaoyan.util.ResponseUtil;
import com.cskaoyan.util.StringUtil;
import lombok.SneakyThrows;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author stone
 * @date 2023/04/05 15:17
 */
@WebServlet("/admin/user/*")
public class AdminUserServlet extends WdBaseServlet {

    UserService userService = new UserServiceImpl();

    public BaseRespVo list(HttpServletRequest request, HttpServletResponse response) {
        String mobile = request.getParameter("mobile");
        Integer userId = StringUtil.isEmpty(request.getParameter("userId")) ? null : Integer.parseInt(request.getParameter("userId"));
        Integer page = Integer.parseInt(request.getParameter("page"));
        Integer limit = Integer.parseInt(request.getParameter("limit"));
        String order = request.getParameter("order");
        String sort = request.getParameter("sort");
        String username = request.getParameter("username");

        CommonData data = userService.query(mobile,userId,page,limit,sort,order,username);
        return BaseRespVo.ok(data);//会转换为json字符，然后通过 response的getWriter去响应
    }

    @SneakyThrows
    private void update (HttpServletRequest request, HttpServletResponse response){

        String line = request.getReader().readLine();
        Map map = JacksonUtil.read(line, Map.class);
        Integer id = (Integer) map.get("id");
        String password =(String) map.get("password");
        String mobile =(String) map.get("mobile");
        Integer gender = (Integer) map.get("gender");
        // byte b = integerObject.byteValue(map.get("gender"));

        String nickname =(String) map.get("nickname");
        Integer status =(Integer) map.get("status");
        Integer userLevel =(Integer) map.get("userLevel");
        userService.updateUser(id,password,mobile,gender,nickname,status,userLevel);

        //wyy修改以下三行，为日志服务
        MarketUser marketUser = new MarketUser();
        marketUser.setId(id);
        marketUser.setNickname(nickname);
        ResponseUtil.responseJson(response, BaseRespVo.ok(marketUser));



    }


    @SneakyThrows
    private void detail(HttpServletRequest request, HttpServletResponse response){

        Integer id = StringUtil.isEmpty(request.getParameter("id")) ? null : Integer.parseInt(request.getParameter("id"));

        Integer page = StringUtil.isEmpty(request.getParameter("page")) ? null :Integer.parseInt(request.getParameter("page"));
        Integer limit = StringUtil.isEmpty(request.getParameter("limit")) ? null :Integer.parseInt(request.getParameter("limit"));
        Integer sort = StringUtil.isEmpty(request.getParameter("sort")) ? null :Integer.parseInt(request.getParameter("sort"));
        Integer order = StringUtil.isEmpty(request.getParameter("order")) ? null :Integer.parseInt(request.getParameter("order"));

        MarketUser data = userService.queryById(id,page,limit,sort,order);
        ResponseUtil.responseJson(response, BaseRespVo.ok(data));
    }

}
